# Copyright:: Copyright (c) 2008 Howling Moon Software


class PreMultAlphaBlendNode < AS::Scene::BlendNode
	def initialize(child)
		super(nil, GL::ONE, GL::ONE_MINUS_SRC_ALPHA, child)
	end
end



class FancyTextNode < AS::Scene::ColorNode

  attr_reader :x, :y, :width, :height

	def initialize(xoffset, yoffset, font_scale, color, text, centered = true, font = AS::Font::DefaultFont)
	
    @width = font.string_width(text)*font_scale
    @height = font.height*font_scale

 		@x = xoffset
		@y = yoffset
 		@x -= @width*0.5 if centered
 		@y -= @height*0.5 if centered
 		
		packed_color = AS::PackedColor.new(color)
		child = PreMultAlphaBlendNode.new{
			matrix = AS::Transform2D.translate(@x, @y)
			matrix *= AS::Transform2D.scale(font_scale, font_scale)
			packed_matrix = AS::PackedMatrix.new(matrix)
			AS::Scene::MatrixNode.new(GL::MODELVIEW, packed_matrix){
				AS::Scene::TextNode.new(font, text)
			}
		}
		super(packed_color, child)
	end	
end

class TextDemo

	def initialize
		super

		font_tex = AS::RectTexture.new(AS::Image.load_png_rgba_premult(AS::LIB_DIR + '/../Demo/textDemo/font.png'))
	  pretty_font = AS::Font.new(font_tex, AS::LIB_DIR + '/../Demo/textDemo/font.metrics')  


		@view_root = AS::Scene::GroupNode.new [
			FancyTextNode.new(20,HEIGHT-70,1, AS::Color::Red, "Test Text. :)", false, pretty_font),
			FancyTextNode.new(WIDTH/2,HEIGHT/2,2.0, AS::Color::Green, "Centered!", true, pretty_font),
			FancyTextNode.new(0,0,1, AS::Color::Red, "(0,0)", false),
			FancyTextNode.new(20,50,0.4, AS::Color::Red, "This is not centered text.. it's just some normal text. floor", false, pretty_font),
		]
	end
	
	def draw
		AS.clear_screen
		@view_root.render
		#AS.print_gl_errors - We took this out of aerosol, as part of some refactoring.
	end
end
